const jwt = require('jsonwebtoken');
const omit = require('lodash/omit')
const { staff_collection } = require('../config')
const { db } = require('../config')
const { command } = require('../config')
const _ = command
const common = require('./common');
exports.main = async (event, context) => {
    const { secret } = event,
        { account, password } = event.data;
    const GET = await db.collection(staff_collection)
        .where({ 
            account: account,
            status:_.neq('离职')
         })
        .get();
    if (!GET.data.length) return { state: 'noPass', msg: '用户名或密码错误或账号不存在' };

    const STAFF = GET.data[0];

    // const isMatch = bcrypt.compareSync(password, STAFF.password);

    let isMatch = password === STAFF.password;
    if (isMatch) {
        var userInfo = omit(STAFF, ['_id', 'password'])
        userInfo = common.simplePosition(userInfo);
        const token = jwt.sign(userInfo, 'shhhhh', { expiresIn: '1h' });
        return { state: 'pass', msg: '登录成功', data: userInfo, token };
    } else {
        return { state: 'noPass', msg: '用户名或密码错误' };
    }

}